<template>
  <div class="tool">
    <div class="tool-item emoji">
      <i class="iconfont icon-smile"
         @click="emojiShow"></i>

      <div class="emoji-warp"
           v-if="isemojiShow">
        <EmojiWarp @sendEmoji="sendEmoji" />
      </div>
    </div>

    <div class="tool-item emoji">
      <i class="iconfont icon-picture"
         @click="imgFileUpload"></i>
      <input type="file"
             style="display:none"
             ref="imgFile"
             @change="uploadImg">
    </div>
  </div>
</template>

<script>
import EmojiWarp from './comps/EmojiWarp'
import {chatImgLoad} from 'server/front_k.js'

export default {
  name:'ChatTools',
  components: {
    EmojiWarp
  },
  data() {
    return {
      isemojiShow: false
    }
  },
  methods: {
    sendEmoji(e) {
      this.$emit('sendEmoji_tool', e)
      this.isemojiShow = false
    },
    emojiShow() {
      this.isemojiShow = true
    },
    imgFileUpload() {
      this.$refs.imgFile.click()
    },
    uploadImg() {
      let file = this.$refs.imgFile.files[0]
      if (!this.$file.img(file.type)) {
        // this.$refs.imgFile.files[0] = ''
        return this.$Message.error('文件格式不符')
      }

      let fromData = new FormData()
      fromData.append('image', file)
      chatImgLoad(fromData).then(res=>{
        this.$emit('imgUploadSuccess', res.data)
        
      })


      //   let _this = this
      //   let reader = new FileReader()
      //   reader.readAsDataURL(file)
      //   reader.onload = function(e) {
      //     _this.imgSrc = this.result
      //   }
    }
  }
}
</script>

<style scoped>
.tool {
  display: flex;
  padding: 2px;
}
.tool-item {
  padding: 5px;
  border-radius: 3px;
}
.tool-item i {
  font-size: 24px;
  cursor: pointer;
}
.tool-item:hover {
  background-color: #f1f1f1;
}
.emoji {
  position: relative;
}
.emoji-warp {
  position: absolute;
  width: 300px;
  height: 200px;
  border: 1px solid #e9e9e9;
  box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2);
  background-color: white;
  top: -205px;
}
</style>